package com.xkcoding.orm.jdbctemplate.Clickhousejdbctemplate.common.utils;

import com.xkcoding.orm.jdbctemplate.annotation.Column;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class TranslatorUtils {
    public static <T> BatchPreparedStatementSetter saveBatchPreparedStatementSetter(final List<T> arg0) {
        final int size = arg0.size();
        if (size < 1) {
            throw new IncorrectResultSizeDataAccessException(1, size);
        }
        return new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int j) throws SQLException {
                try {
                    Object[] param = saveParam(arg0.get(j));
                    int paramsize = param.length;
                    for (int i = 0; i < paramsize; i++) {
                        if (param[i] instanceof Long) {
                            ps.setLong(i + 1, (Long) param[i]);
                        } else if (param[i] instanceof String) {
                            ps.setString(i + 1, (String) param[i]);
                        } else if (param[i] instanceof BigDecimal) {
                            ps.setBigDecimal(i + 1, (BigDecimal) param[i]);
                        } else if (param[i] instanceof Integer) {
                            ps.setInt(i + 1, (Integer) param[i]);
                        } else if (param[i] instanceof Float) {
                            ps.setFloat(i + 1, (Float) param[i]);
                        } else if (param[i] instanceof Double) {
                            ps.setDouble(i + 1, (Double) param[i]);
                        } else if (param[i] instanceof Date) {
                            ps.setDate(i + 1, (Date) param[i]);
                        } else if (param[i] instanceof java.util.Date) {
                            java.util.Date date = (java.util.Date) param[i];
                            Timestamp sqlDate = new Timestamp(date.getTime());
                            ps.setTimestamp(i + 1, sqlDate);
                        } else if (param[i] instanceof Boolean) {
                            ps.setBoolean(i + 1, (Boolean) param[i]);
                        } else if (param[i] instanceof Time) {
                            ps.setTime(i + 1, (Time) param[i]);
                        } else if (param[i] instanceof Timestamp) {
                            ps.setTimestamp(i + 1, (Timestamp) param[i]);
                        } else if (param[i] instanceof Short) {
                            ps.setShort(i + 1, (Short) param[i]);
                        } else if (param[i] instanceof Clob) {
                            ps.setClob(i + 1, (Clob) param[i]);
                        } else {
                            ps.setNull(i + 1, Types.INTEGER);
                        }
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public int getBatchSize() {
                return size;
            }
        };
    }

    private static Object[] saveParam(Object arg1) throws IllegalArgumentException, IllegalAccessException {
        List<Object> arg10 = new ArrayList<Object>();
        Class<?> c = arg1.getClass();
        Field[] fs = c.getDeclaredFields();
        for (Field field : fs) {
            Column column = field.getAnnotation(Column.class);
          //  Id id = field.getAnnotation(Id.class);
            if (column != null) {
                    field.setAccessible(true);
                    arg10.add(field.get(arg1));
            }
        }
        return arg10.toArray();
    }
}
